{% extends 'website/backend/backend_base.html' %}
{% load static %}
{% block option-title %}
    修改碎碎念
{% endblock %}
{% block content %}
    <div class="col-md-12">
        <form class="form-horizontal" id="data-form">
            <div class="form-group">
                <label for="title" class="col-sm-1 control-label">标题:</label>
                <div class="col-sm-9">
                    <input type="text" id="title" name="title" class="form-control" value="{{ mood.title }}">
                </div>
            </div>

             <div class="form-group">
                <label for="mood_type" class="col-sm-1 control-label">类型:</label>
                <div class="col-sm-9">
                    <select name="mood_type" id="mood_type" class="form-control">
                        <option value="T" {% if mood.mood_type == 'T' %}selected{% endif %}>文字</option>
                        <option value="I" {% if mood.mood_type == 'I' %}selected{% endif %}>图文</option>
                        <option value="B" {% if mood.mood_type == 'B' %}selected{% endif %}>引用</option>
                    </select>
                </div>
            </div>

             <div class="form-group">
                <label for="content" class="col-sm-1 control-label">内容:</label>
                <div class="col-sm-9">
                    <textarea id="content" name="content" class="form-control" rows="5" wrap="hard">{{ mood.content }}</textarea>
                </div>
            </div>

             <div class="form-group">
                <label for="img" class="col-sm-1 control-label">配图:</label>
                <div class="col-sm-9">
                    <input type="file" id="image" name="image" class="form-control">
                    {% if mood.image %}
                        <span class="help-block" id="helpBlock">当前图片：<a href="{% get_media_prefix %}{{ mood.image }}">{% get_media_prefix %}{{ mood.image }},不做修改则上面留空</a></span>
                    {% endif %}
                </div>
            </div>
            <button class="btn btn-info" id="submit-btn">修改</button>
            <input type="reset" name="reset" style="display: none">
        </form>
    </div>
    {% include 'utils/modal.html' %}
{% endblock %}

{% block js %}
    <script type="text/javascript">
        $('#submit-btn').click(function (e){
            e.preventDefault()
            var options = {
                type:"POST",
                dataType:'json',
                url:"{% url 'mood-update' mood.id %}",
                success:function (data){
                    var state = data.state
                    if (state === 'success'){
                        showModal('success','修改成功')
                    }else {
                        showModal('danger','修改失败')
                    }
                    $('input[type=reset]').trigger('click')
                },
                error:function (data){
                    console.log(data)
                }
            }
            $('#data-form').ajaxSubmit(options)
        })
    </script>
{% endblock %}